import Vue from 'vue';

const files = require.context('@/api/service/', true, /\.js$/);
const service = {};

files.keys().forEach(key => {
	if (key.split('/').length !== 3) {
		Promise.reject('file error' + key + ' view service folders Refer to test folder ');
		return;
	}
	const item = files(key).default;

	let keySplit = key
		.split('/')
		.filter(item => {
			return item.indexOf('.js') !== -1;
		})[0]
		.replace('.js', '');

	let ObjectName = key.split('/')[1];
	try {
		if (service[ObjectName]) {
			service[ObjectName][keySplit] = new item();
		} else {
			service[ObjectName] = {};
			service[ObjectName][keySplit] = new item();
		}
	} catch {
		Promise.reject('file error' + key + ' view service folders Refer to test folder ');
	}
});
Vue.prototype.$service = service;
export default service;
